<?php


class PackageController extends MainController implements ControllerInterface  {
	
	public function preDispatch($param) {
		$this->Run($param);
		$this->RunShared('Auth');
		$this->smarty->assign('timestamp', microtime() );
	}
	
	public function postDispatch()
	{
		
	}
	
	public function IndexAction($param)
	{
		$table_data = $this->getTableData('package_table', $param, array('o' => 'name', 'v' => 'malejaco', 't' => 10));
		$v = $table_data['v'] == 'malejaco' ? 'DESC' : 'ASC';
		$oTable = array(
			'name' => 'package.name',
			'days' => 'package.days',
			'price' => 'package.price',
			'status' => 'package.active'
		);
		$o = $oTable[$table_data['o']];
		$p = isset($param['strona']) ? $param['strona'] : 1;

		$dalData = PackageDAL::GetDalDataObj(
						array(
							'count' => true,
							'condition' => array(
							//'parent'=> array('NULL'),
							),
						)
		);
		$this->smarty->assign('c', PackageDAL::getResult($dalData));

		$dalData->setSortBy("$o $v");
		$dalData->setCount(false);
		$dalData->setLimit((($p - 1) * $table_data['t']) . ', ' . $table_data['t']);


		$arrayObjPackages = PackageDAL::GetResult($dalData);

		$this->smarty->assign('arrayObjPackages', $arrayObjPackages);		
	}

	public function AddAction($param)
	{
		$data = array(
			'name' => null,
			'days' => null,
			'price' => null,
			'active' => 0
		);
		
		
		if(Request::IsPost())
		{
			$errors = array();
			$valid = 1;
			$data = Request::GetAllPost();
			// walidacja
			if(empty($data['name']))
			{
				$errors['name'] = 'Pole nie może być puste';
				$valid = 0;
			}
			
			if(empty($data['days']) || ((int)trim($data['days']) < 1) || ((int)trim($data['days']) != trim($data['days'])))
			{
				$errors['days'] = 'Nieprawidłowa wartość (oczekiwano wartości liczbowej)';
				$valid = 0;
			}
			
			if(empty($data['price']) || (floatval($data['price']) < 1) || (floatval(trim($data['price'])) != trim($data['price'])))
			{
				$errors['price'] = 'Nieprawidłowa wartość (oczekiwano wartości liczbowej)';
				$valid = 0;
			}
			
			if($valid)
			{
				$package = new Package();
				$package->setName($data['name']);
				$package->setDays($data['days']);
				$package->setPrice($data['price']);
				$package->setActive($data['active']);
				PackageDAL::Save($package->GetDalDataObj());
				
				return $this->AddRedirect(Router::GenerateUrl('params', array('Package'=>'Index')), 0);
			} 
			$this->smarty->assign('error', $errors);
			
		}	
		
		$this->smarty->assign('data', $data);
		
	}	
	
	public function EditAction($param)
	{
		//
		if($param['id'])
		{
			$obj = array_pop(PackageDAL::GetResult(PackageDAL::GetDalDataObj(array('limit'=>1, 'condition'=>array('id_package'=>array($param['id']))))));
		}
		if(empty ($obj) || !isset($param['id']))
		{
			$this->addRedirect(Router::GenerateUrl('packageIndex',array('Package' => 'Index')),0);
		}
		
		if(Request::IsPost())
		{
			$data = Request::GetAllPost();
			$obj->setname($data['name']);
			$obj->setName($data['name']);
			$obj->setDays($data['days']);
			$obj->setPrice($data['price']);
			$obj->setActive($data['active']);
			PackageDAL::Save($obj->GetDalDataObj());

			return $this->AddRedirect(Router::GenerateUrl('params', array('Package'=>'Index')), 0);		
		}
		$this->smarty->assign('obj', $obj);
	}
	
	public function DeleteAction($param)
	{
		$this->setNoRender();
		if($param['id'])
		{
			$man = array_pop(PackageDAL::GetResult(PackageDAL::GetDalDataObj(array('limit'=>1, 'condition'=>array('id_package'=>array($param['id']))))));
		}
		
		if(empty ($man) || !isset($param['id']))
		{
			$this->addRedirect(Router::GenerateUrl('packageIndex',array('Package' => 'Index')),0);
		}
		
		PackageDAL::Delete($man->getDalDAtaObj());
		return $this->addRedirect(Router::GenerateUrl('packageIndex',array('Package' => 'Index')),0);		
	}
	
	public function ToggleStatusAction($param)
	{
		try {
			$this->SetAjaxRender();
			$dalObj = PackageDAL::GetDalDataObj(array('id' => $param['id']));
			$this->content = PackageDAL::DefaultToggleFieldById($dalObj, 'active');
		} catch (Exception $e) {
			echo $e->getMessage();
		}		
	}
	
}